import 'dart:async';
import 'dart:math';

import 'package:flutter/material.dart';

class CardsList2Page extends StatefulWidget {
  const CardsList2Page({super.key});

  @override
  State<CardsList2Page> createState() => _CardsList2PageState();
}

class _CardsList2PageState extends State<CardsList2Page> {


  late Timer _timer;

  // Card编辑起始位置
  int editStartIndex = 0;

  // 号码种子
  List<List<int>> cardNums = [
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],
  ];

  int inputModel = 1;


  String noWinningNums = "";

  TextEditingController controller = TextEditingController();

  @override
  void initState() {
    super.initState();
    controller.addListener(() {
      if(controller.value.text.length >= 2) {
        if(1 == inputModel) {
          goNextInput();
        } else {
          goNextInput();
        }

      }
    });
  }

  goNextInput() {
    setState(() {
      cardNums[0][editStartIndex] = int.parse(controller.value.text.toString());
      editStartIndex = editStartIndex+1 < 25 ? editStartIndex+1 : 24;
    });
    _timer = Timer.periodic(const Duration(milliseconds: 300) , (timer){
      controller.clear();
      _timer.cancel();
    });

  }


  // goNextInput() {
  //   setState(() {
  //     cardNums[0][editStartIndex] = int.parse(controller.value.text.toString());
  //     editStartIndex = editStartIndex+1 < 25 ? editStartIndex+1 : 24;
  //   });
  //   _timer = Timer.periodic(const Duration(milliseconds: 300) , (timer){
  //     controller.clear();
  //     _timer.cancel();
  //   });
  //
  // }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('BingGo Bomb', style: TextStyle(color: Colors.black87),),
        backgroundColor: Colors.transparent,
        elevation: 0.0,
        centerTitle: true,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            buildCard(),
            TextField(
              textInputAction: TextInputAction.go,
              controller: controller,
              onChanged: (v) {
                print("onChanged：${v}");
              },
              onEditingComplete: () {
                print("onEditingComplete");
              },
              onSubmitted: (v) {
                print("onSubmitted：${v}");
              },
              onTap: () {
                print("onTap");
              },
              onTapOutside: (e) {
                print(e);
              },
            ),
            Text(noWinningNums)

          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: (){},
        tooltip: 'Increment',
        child: Row(
          children: [Icon(Icons.ac_unit_outlined), Text("切换")],
        ),
      ),
    );
  }

  Widget buildCard() {
    return Card(
      color: Colors.black,
      elevation: 4,
      shape: RoundedRectangleBorder(),
      child: Container(
        width: 200,
        height: 200,
        margin: EdgeInsets.only(left: 2, top: 2,right: 2, bottom: 2),
        child: GridView.count(
          crossAxisCount: 5,
          mainAxisSpacing: 2,
          crossAxisSpacing: 2,
          children: buildItem(0),
        ),
      ),
    );
  }

  List<Widget> buildItem(index) {
    List<Widget> listWidgets = [];
    List<int> cardNum = [];
    for(int i = 0;i < cardNums[index].length; i++) {
      int n = Random().nextInt(100);
      n = 0;
      cardNum.add(n);
      listWidgets.add(
          InkWell(
            child: Container(
              width: 30,
              height: 30,
              color: Colors.pinkAccent,
              alignment: Alignment.center,
              child: Text( cardNums[index][i].toString(),
                style: TextStyle(
                    color: Colors.black,
                    fontSize: 24,
                    fontWeight: FontWeight.bold
                ),
              ),
            ),
            onTap: () {
              print('你点击我了。');
              cardItemTap(i);
            },
          )
      );
    }
    cardNums.add(cardNum);
    return listWidgets;
  }


  cardItemTap(index) {
    print(index);
    setState(() {
      editStartIndex = index;
    });
  }


  List<List<int>> getWinningNums(index) {
    List<List<int>> winningNums = [];
    List<int> numsSeed = cardNums[index];
    winningNums.add([numsSeed[0], numsSeed[1], numsSeed[2], numsSeed[3], numsSeed[4]]);
    winningNums.add([numsSeed[5], numsSeed[6], numsSeed[7], numsSeed[8], numsSeed[9]]);
    winningNums.add([numsSeed[10], numsSeed[11], numsSeed[12], numsSeed[13], numsSeed[14]]);
    winningNums.add([numsSeed[15], numsSeed[16], numsSeed[17], numsSeed[18], numsSeed[19]]);
    winningNums.add([numsSeed[20], numsSeed[21], numsSeed[22], numsSeed[23], numsSeed[24]]);

    winningNums.add([numsSeed[0], numsSeed[5], numsSeed[10], numsSeed[15], numsSeed[20]]);
    winningNums.add([numsSeed[1], numsSeed[6], numsSeed[11], numsSeed[16], numsSeed[21]]);
    winningNums.add([numsSeed[2], numsSeed[7], numsSeed[12], numsSeed[17], numsSeed[22]]);
    winningNums.add([numsSeed[3], numsSeed[8], numsSeed[13], numsSeed[18], numsSeed[23]]);
    winningNums.add([numsSeed[4], numsSeed[9], numsSeed[14], numsSeed[19], numsSeed[24]]);

    winningNums.add([numsSeed[0], numsSeed[6], numsSeed[12], numsSeed[18], numsSeed[24]]);
    winningNums.add([numsSeed[4], numsSeed[8], numsSeed[12], numsSeed[16], numsSeed[20]]);

    return winningNums;
  }


}
